$(function () {
    let layer = layui.layer
    // 需求1：渲染文章列表
    initArtCateList()
    function initArtCateList() {
        axios({
            method: 'GET',
            url: '/my/article/cates',
        }).then(res => {
            // console.log(res.data);
            if (res.data.status !== 0) {
                return layer.msg(res.data.message)
            }
            // 渲染文章分类列表
            let arr = []
            $.each(res.data.data, function (index, item) {
                arr.push(`
                    <tr>
                            <td>${item.Id}</td>
                            <td>${item.name}</td>
                            <td>${item.alias}</td>
                            <td>
                                <button data-id = "${item.Id}" class=" btnEdit layui-btn layui-btn-xs">修改</button>
                                <button class="btnDel layui-btn layui-btn-danger layui-btn-xs">删除</button>
                            </td>
                    </tr>
                `)
            })
            $("tbody").empty().html(arr.join(''))
        })
    }
    let indexAdd
    // 需求2：点击添加显示弹出层
    $('#add').on('click', function () {
        indexAdd = layer.open({
            title: '添加文章分类',
            type: 1,
            area: ['500px', '250px'],
            content: `
                <form id="formAdd" class="layui-form" action="">
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" required lay-verify="required" placeholder="请输入分类名称" autocomplete="off"
                                class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类别名</label>
                        <div class="layui-input-block">
                            <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off"
                                class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit lay-filter="formDemo">确认添加</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>            
            `
        });
    })

    // 需求3：点击确认添加按钮，添加文章分类
    $('body').on('submit', '#formAdd', function (e) {
        e.preventDefault()
        // 发送ajax
        axios({
            method: 'POST',
            url: '/my/article/addcates',
            data: $(this).serialize()
        }).then(res => {
            if (res.data.status !== 0) {
                return layer.msg(res.data.message)
            }
            layer.msg('恭喜您！添加成功')
            initArtCateList()
            // 关闭对话框
            layer.close(indexAdd)
        })
    })

    // 需求4：修改分类的弹出层
    let indexEdit
    $('tbody').on('click', '.btnEdit', function () {
        // 点击弹出层
        indexEdit = layer.open({
            title: '修改文章类别',
            type: 1,
            area: ['500px', '250px'],
            content: `
                <form id="formEdit" class="layui-form" lay-filter="editForm" action="">
                    <input type="hidden" name = "Id">
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" required lay-verify="required" placeholder="请输入分类名称" autocomplete="off"
                                class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类别名</label>
                        <div class="layui-input-block">
                            <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off"
                                class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit lay-filter="formDemo">确认修改</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>            
            `
        });
        // 弹出层赋值
        let id = $(this).attr('data-id')
        // 发送ajax
        axios({
            method: 'GET',
            //  /：id 动态参数（路由参数）
            // 特点：/不能省略， ：一定要去掉，id是动态变化的
            url: '/my/article/cates/' + id
        }).then(res => {
            if (res.data.status !== 0) {
                return layer.msg(res.data.message)
            }
            layui.form.val('editForm', res.data.data)
        })

    })

    // 需求5：修改文章分类
    $('body').on('submit', '#formEdit', function (e) {
        e.preventDefault()
        axios({
            method: 'POST',
            url: '/my/article/updatecate',
            data: $(this).serialize()
        }).then(res => {
            if (res.data.status !== 0) {
                return layer.msg(res.data.message)
            }
            layer.msg('恭喜您！修改成功')
            initArtCateList()
            // 关闭对话框
            layer.close(indexEdit)
        })
    })

    // 需求6：删除文章分类
    $('tbody').on('click', '.btnDel', function () {
        let id = $(this).siblings().attr('data-id')
        // 询问框询问是否删除
        layer.confirm('是否删除？', { icon: 3, title: '提示' }, function (index) {
            //发送ajax
            axios({
                method: 'GET',
                url: '/my/article/deletecate/' + id
            }).then(res => {
                if (res.data.status !== 0) {
                    return layer.msg(res.data.message)
                }
                layer.msg('删除成功！')
                initArtCateList()
            })
            layer.close(index);
        });

    })




})